1023C - Bracket Subsequence - CodeForces Solution


greedy *1200

Please click on ads to support us..

Python Code:

n,k=map(int,input().split())
s=input()
c=0
y=0
y1=0
c1=0
l=[]
'''for x in s:
    if x=="(":
        c=c+1
    else:
        c1=c1+1'''
y=k//2
y1=k//2
a=0
b=0
for x in s:
    if x == "(" and a<y:
        l.append(x)
        a=a+1
    if x==")" and b<y1:
        l.append(x)
        b=b+1
for x in l:
    print(x,end="")
        
        

C++ Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)

void solve(){
	int n,k;
	cin>>n>>k;
	string s;
	cin>>s;
	string ans;
	ans="";
	int imb=0;
	rep(i,0,n){
		if(s[i]==')' && imb>0){
			ans+=s[i];
			imb--;
		}else{
			if((int)ans.size()+1+imb+1<=k){
				ans+=s[i];
				imb++;
			}
		}
	}
	cout<<ans<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	//cin>>tt;
	while(tt--){
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety